package ca.etsmtl.log430.lab1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

//import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

/**
 * This class defines the Resource object for the system. Besides the basic
 * attributes, there are two lists maintained. alreadyAssignedProjectList is a
 * ProjectList object that maintains a list of projects that the resource was
 * already assigned to prior to this execution of the system.
 * projectsAssignedList is also a ProjectList object that maintains a list of
 * projects assigned to the resource durint the current execution or session.
 * 
 * @author A.J. Lattanze, CMU
 * @version 1.6, 2013-Sep-13
 */

/* Modification Log
 ****************************************************************************
 * v1.6, R. Champagne, 2013-Sep-13 - Various refactorings for new lab.
 * 
 * v1.5, R. Champagne, 2012-Jun-19 - Various refactorings for new lab.
 * 
 * v1.4, R. Champagne, 2012-May-31 - Various refactorings for new lab.
 * 
 * v1.3, R. Champagne, 2012-Feb-02 - Various refactorings for new lab.
 * 
 * v1.2, 2011-Feb-02, R. Champagne - Various refactorings, javadoc comments.
 *  
 * v1.1, 2002-May-21, R. Champagne - Adapted for use at ETS. 
 * 
 * v1.0, 12/29/99, A.J. Lattanze - Original version.

 ****************************************************************************/

public class Resource {

	SimpleDateFormat formatter;
	
	/**
	 * Resource's last name
	 */
	private String lastName;
	
	/**
	 * Resource's first name
	 */
	private String firstName;
	
	/**
	 * Resource's identification number
	 */
	private String id;
	
	/**
	 * Resource role 
	 */
	private String role;

	/**
	 *  List of projects the resource is already allocated to
	 */
	private ProjectList alreadyAssignedProjectList = new ProjectList();

	/**
	 *  List of projects assigned to the resource in this session
	 */
	private ProjectList projectsAssignedList = new ProjectList();
	
	/**
	 *  List of projects the resource is already allocated to concatenanted with
	 *  List of projects assigned to the resource in this session
	 */
	private ProjectList AllProjectsList = new ProjectList();


	/**
	 * Assigns a project to a resource.
	 * 
	 * @param project
	 */
	public void assignProject(Project project) {

		getProjectsAssigned().addProject(project);

	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setID(String id) {
		this.id = id;
	}

	public String getID() {
		return id;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getRole() {
		return role;
	}

	public void setPreviouslyAssignedProjectList(ProjectList projectList) {
		this.alreadyAssignedProjectList = projectList;
	}

	public ProjectList getPreviouslyAssignedProjectList() {
		return alreadyAssignedProjectList;
	}

	public void setProjectsAssigned(ProjectList projectList) {
		this.projectsAssignedList = projectList;
	}
	
	public ProjectList getProjectsAssigned() {
		return projectsAssignedList;
	}
	
	public ProjectList getAllProjectsAssigned() {
	
		
		boolean done = false;
		
		AllProjectsList.emptyList();
		this.alreadyAssignedProjectList.goToFrontOfList();
		this.projectsAssignedList.goToFrontOfList();
		
		while(!done){
			
			Project projet = this.alreadyAssignedProjectList.getNextProject();
			if (projet !=null){
			this.AllProjectsList.addProject(projet);
			}
			 if( projet == null){
				Project nouveauProjet = this.projectsAssignedList.getNextProject();
				if (nouveauProjet !=null){
				this.AllProjectsList.addProject(nouveauProjet);}
				 if( nouveauProjet == null ){
						
						done=true;
						}
			}
		}//end while	
		return AllProjectsList;
	}
	
	public boolean isOverloaded(ProjectList pl){
		boolean isOverloaded=false;
		boolean done = false;
		boolean doneProjets = false;
		formatter = new SimpleDateFormat("yyyy-MM-dd");
		
		Date firstDate = new Date();
		Date lastDate = new Date();
		
		ProjectList allAssignedProjects = getAllProjectsAssigned();

		normalizeProjectList(pl, allAssignedProjects);
		allAssignedProjects.goToFrontOfList();
		
		
		//Finding the very start date of all projects when it is after today, if there's not, it's today
		//Finding the very end date of all projects when they are not already ended
		while(!done){
			
			Project projet = allAssignedProjects.getNextProject();
			
			if( projet == null){
				done=true;
			}
			else{
				try {
					Date today = new Date();
					Date startProjet = formatter.parse(projet.getStartDate());
					Date endProjet = formatter.parse(projet.getEndDate());
					//System.out.println(endDate);
					if(endProjet.after(lastDate)){
						lastDate = endProjet;
						
					}
					else if(startProjet.before(firstDate) && startProjet.after(today)){
						firstDate = startProjet;
						
					}

				} //fin try

				catch (java.text.ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}//fin else
			
		}//fin while
		
		Calendar start = Calendar.getInstance();
        start.setTime(firstDate);
        Calendar end = Calendar.getInstance();
        end.setTime(lastDate);
        
        //For everyday between the very first date and the very end date,
        //checking the sum of the priority of all projects assigned to the resource
        //if, at one day given, the priority is >100, then the resource is overloaded
        while(start.get(Calendar.DAY_OF_MONTH)!=end.get(Calendar.DAY_OF_MONTH)
        		|| start.get(Calendar.MONTH)!=end.get(Calendar.MONTH) 
        		|| start.get(Calendar.YEAR)!=end.get(Calendar.YEAR)){
        	
        	allAssignedProjects.goToFrontOfList();
        	doneProjets=false; 	
        	
        	int dayPriority=0;
        	
        	while(!doneProjets){
        		Project projet = allAssignedProjects.getNextProject(); 
        		
        		if(projet==null){
        			doneProjets=true;
        		}
        		else{
        			try {
        				Calendar startP = Calendar.getInstance();
						startP.setTime(formatter.parse(projet.getStartDate()));
						Calendar endP = Calendar.getInstance();
	        	        endP.setTime(formatter.parse(projet.getEndDate()));
	        	        
	        	        if(start.getTime().after(startP.getTime()) && start.getTime().before(endP.getTime()) ){
	        	        	dayPriority+= projet.getNumericPriority();
	        	        	
	        	        }
	        	        
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
        	        
        			
        		}
        	}
        	
        	if(dayPriority>100){
        		isOverloaded=true;
        		break;
        	}
        	else{
        		isOverloaded=false;
        	}
        	start.add(Calendar.DAY_OF_MONTH, 1);
        }
      
		return isOverloaded;
	}

	public boolean isOverloadedWith(Project p, ProjectList pl){
		boolean isOverloaded=false;
		boolean done = false;
		boolean doneProjets = false;
		formatter = new SimpleDateFormat("yyyy-MM-dd");
		
		Date firstDate = new Date();
		Date lastDate = new Date();
		
		ProjectList allAssignedProjects = getAllProjectsAssigned();
		allAssignedProjects.addProject(p);
		normalizeProjectList(pl, allAssignedProjects);
		allAssignedProjects.goToFrontOfList();
		
		
		//Finding the very start date of all projects when it is after today, if there's not, it's today
		//Finding the very end date of all projects when they are not already ended
		while(!done){
			
			Project projet = allAssignedProjects.getNextProject();
			
			if( projet == null){
				done=true;
			}
			else{
				try {
					Date today = new Date();
					Date startProjet = formatter.parse(projet.getStartDate());
					Date endProjet = formatter.parse(projet.getEndDate());
					if(endProjet.after(lastDate)){
						lastDate = endProjet;
						
					}
					else if(startProjet.before(firstDate) && startProjet.after(today)){
						firstDate = startProjet;
						
					}

				} //fin try

				catch (java.text.ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}//fin else
			
		}//fin while
		
		Calendar start = Calendar.getInstance();
        start.setTime(firstDate);
        Calendar end = Calendar.getInstance();
        end.setTime(lastDate);
        
        //For everyday between the very first date and the very end date,
        //checking the sum of the priority of all projects assigned to the resource
        //if, at one day given, the priority is >100, then the resource is overloaded
        while(start.get(Calendar.DAY_OF_MONTH)!=end.get(Calendar.DAY_OF_MONTH)
        		|| start.get(Calendar.MONTH)!=end.get(Calendar.MONTH) 
        		|| start.get(Calendar.YEAR)!=end.get(Calendar.YEAR)){
        	
        	allAssignedProjects.goToFrontOfList();
        	doneProjets=false; 	
        	
        	int dayPriority=0;
        	
        	while(!doneProjets){
        		Project projet = allAssignedProjects.getNextProject(); 
        		
        		if(projet==null){
        			doneProjets=true;
        		}
        		else{
        			try {
        				Calendar startP = Calendar.getInstance();
						startP.setTime(formatter.parse(projet.getStartDate()));
						Calendar endP = Calendar.getInstance();
	        	        endP.setTime(formatter.parse(projet.getEndDate()));
	        	        
	        	        if(start.getTime().after(startP.getTime()) && start.getTime().before(endP.getTime()) ){
	        	        	dayPriority+= projet.getNumericPriority();
	        	        	
	        	        }
	        	        
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
        	        
        			
        		}
        	}
        	if(dayPriority>100){
        		isOverloaded=true;
        		break;
        	}
        	else{
        		isOverloaded=false;
        	}
        	start.add(Calendar.DAY_OF_MONTH, 1);
        }
      
        allAssignedProjects.removeLast();
		return isOverloaded;
	}
	
	
	
	
	/**
	 * Receive the list of all loaded projects,
	 * find in it each project in the previously assigned list and copies the missing information: 
	 * name, startDate, endDate, priority.
	 * 
	 * @param projectList
	 */
	public void normalizePreviouslyAssignedProjectList(ProjectList projectList) {
		
		boolean done;
		Project project;
		
		getPreviouslyAssignedProjectList().goToFrontOfList();
		done = false;

		while (!done) {

			project = getPreviouslyAssignedProjectList().getNextProject();

			if (project == null) {

				done = true;

			} else {

				// Find the project in the project's list
				Project matchingProject = projectList.findProjectByID(project.getID());
				
				if (matchingProject != null) {
					
					// Copy the project's information
					project.setProjectName(matchingProject.getProjectName());
					project.setStartDate(matchingProject.getStartDate());
					project.setEndDate(matchingProject.getEndDate());
					project.setPriority(matchingProject.getPriority());
					
				}

			} // if

		} // while
	}
	
	public void normalizeProjectList(ProjectList projectList, ProjectList wantedProjectList) {
	
		wantedProjectList.goToFrontOfList();
		
		boolean done = false, doneP=false;
		while(!done){
			Project project = wantedProjectList.getNextProject();
			if(project==null){
				done=true;
			}
			else{
				projectList.goToFrontOfList();
				while(!doneP){
					Project projectAll = projectList.getNextProject();
					if(projectAll==null){
						doneP=true;
					}
					else{
						if( project.getID().equals(projectAll.getID()) ){
							project.setPriority(projectAll.getPriority());
							project.setProjectName(projectAll.getProjectName());
							project.setStartDate(projectAll.getStartDate());
							project.setEndDate(projectAll.getEndDate());
						}
					}

				}
			}
			doneP=false;
		}
		
		
	}

} // Resource class
